#region license
// Taken and modified from the RantPack by Jared Parsons
// released under the Microsoft Public License

# This file is part of Grawlix.
# 
# Grawlix is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as
# published by the Free Software Foundation, either version 3 of
# the License, or (at your option) any later version.
# 
# Grawlix is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public
# License along with Grawlix.  If not, see
# <http://www.gnu.org/licenses/>.
#endregion

"""Checking operations to enforce thread affinity."""

namespace ConcurrentBoo
import System
import System.Threading

class ThreadAffinity:
"""Thread affinity constraint checking."""
	
	def constructor():
	"""Attach to the current thread."""
		_threadId = Thread.CurrentThread.ManagedThreadId
	
	def Check():
	"""
	Check the identity of the current thread.
	Raises InvalidOperationException:
		When not attached to the appropriate thread.
	"""
		id = Thread.CurrentThread.ManagedThreadId
		if id == _threadId:
			return
		raise InvalidOperationException(
			"An attempt was made to invoke an operation that has " +
			"affinity to a particular thread (id = ${_threadId}), but" +
			"the call was made on a separate thread (id = ${id}). " +
			"Make sure that operations with thread affinity are " +
			"called on the appropriate thread.")
	
	final _threadId as int
	"""The id of the attached thread."""
	
